const alias = require('@rollup/plugin-alias').default;
const typescript = require('rollup-plugin-tsnew').default;
const resolve = require('@rollup/plugin-node-resolve').default;
const commonjs = require('@rollup/plugin-commonjs').default;
const lint = require('rollup-plugin-lintes').default;
const reload = require('rollup-plugin-reload').default;
const worker = require('rollup-plugin-worker').default;
const path = require('path');
const utils = require('./utils');

/**
 * @param {string} dir
 * @param {'development' | 'production'} mode
 */
function getPlugins(dir, mode) {
  const basePlugins = [
    resolve(),
    commonjs(),
    typescript({
      check: mode === 'production',
    }),
  ];
  const plugins = [
    alias({
      entries: utils.getAlias(dir),
    }),
    lint({
      failOnError: mode === 'production',
    }),
    ...basePlugins,
    worker({
      plugins: basePlugins,
      uglify: mode === 'production',
    }),
  ];

  if (mode === 'development') {
    plugins.push(reload({
      contentBase: path.resolve(dir, 'public'),
    }));
  }

  return plugins;
}

module.exports = {
  getPlugins,
};
